refactor(decoders): per-instance cache replaces module lru_cache#42
Merged
Conversation
Move TypeAdapter / msgspec.json.Decoder caching from `@functools.lru_cache` module-level functions onto per-instance dicts on each decoder. No public API change; tied to decoder/client lifecycle; removes the autouse cache-clear test fixtures and the implicit cross-test pollution risk they were guarding against.
Three-task TDD sequence: PydanticDecoder per-instance _adapters dict, then MsgspecDecoder per-instance _msgspec_decoders dict, then the engineering.md Seam B description update.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PydanticDecoder._adaptersandMsgspecDecoder._msgspec_decodersinstance dicts replace module-level@functools.lru_cachefactories. Cache lifetime now matches the decoder/client, not the process._clear_adapter_cache(test_decoders_pydantic.py) and_clear_msgspec_cache(test_decoders_msgspec.py, added one PR ago in PR feat!: multi-decoder routing (decoders=[...]) #41).No public API change. Folded into 0.9.0 (untagged).
Spec:
planning/specs/2026-06-10-decoder-instance-cache-design.mdPlan:
planning/plans/2026-06-10-decoder-instance-cache-plan.mdTest plan
just lintclean (ruff format + ruff check + ty)just testgreen — 485 passed, 100% coveragedecode()preserved; tests usepatch.object(PydanticDecoder, '_get_adapter', ...)andpatch.object(MsgspecDecoder, '_get_msgspec_decoder', ...)instead of patching module-level functionscan_decode_uses_cacheassertions shifted from_get_adapter.cache_info()to introspectingdecoder._adapters/decoder._msgspec_decodersdicts directlyMsgspecDecoder.can_decode'smsgspec.inspect.type_info+CustomTypefilter preserved verbatim (orthogonal to cache mechanics)planning/engineering.mdSeam B description updated to reflect per-instance ownership